package com.pur.zsms.common.dao;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceContextType;

/**
 * <p>抽象基类，被实际的JPA DAO继承。其子类可以方便的使用JpaTemplate进行数据操作。</p>
 * <p>该类的EntityManager一般通过Spring的PersistenceAnnotationBeanPostProcessor自动注入。</p>
 * <p>Create Time: 2008-10-27</p>
 * @author wangzg002
 * 
 */
public abstract class JpaDaoSupport {
	
	/**
	 * JPA的EntityManager实例,由spring注入
	 */
	@PersistenceContext(type = PersistenceContextType.TRANSACTION)
	protected EntityManager entityManager;
	/**
	 * 封装的实现模板
	 */
	private JpaTemplate template;
	
	/**注入EntityManager
	 * @param entityManager EntityManager
	 */
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
		
	/**获取模板,供DAO子类实现业务方法
	 * @return jpa实现模板实例
	 */
	protected JpaTemplate getTemplate() {
		if(this.template == null){
			template=new JpaTemplate(this.entityManager);
		}
		return template;
	}
	

	/**获取JPA的EntityManager,供DAO直接使用JPA的方法
	 * @return JPA EntityManager
	 */
	protected EntityManager getEntityManager() {
		return entityManager;
	}


}
